Skip to content

Conversation

@henryjcee
Copy link

  • Do only one thing
  • Non breaking API changes
  • [?] Tested

What did this pull request do?

We're having issues reading a row into a struct with nullable fields, represented by the guregu/null.v3 types; specifically in the case where the columns in the table have been updated from non-null to null. e.g.

err := tx.Model(target).
		WithContext(ctx).
		Where(target, "ID").
		Preload(clause.Associations).
		First(target).
		Error

In this case we'd expect the struct field to be set to null.Time{} but what we find is that the field is left unchanged. I've tracked the issue down to this commit and I can't make out what the intention of the change by looking at the previous logic:

} else if reflectV.Kind() == reflect.Ptr {
	if reflectV.IsNil() || !reflectV.IsValid() {
		field.ReflectValueOf(ctx, value).Set(reflect.New(field.FieldType).Elem())
	} else {
		return field.Set(ctx, value, reflectV.Elem().Interface())
	}
}

The commit in question mentions fixing a problem related to time fields but this block looks unrelated.

@henryjcee henryjcee changed the title Null fields are ignored when scanning to a model struct WIP - Null fields are ignored when scanning to a model struct Oct 10, 2024
@henryjcee
Copy link
Author

Closing this in favour of an issue now that I understand the problem

@henryjcee henryjcee closed this Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant